#include "queue.h"

MyQueue::MyQueue()
{
	this->_front = NULL;
	this->_rear = NULL;
}

MyQueue::QueuePush(QDataType data)
{
	try
	{
		QNode* temp = new QNode();
		temp->_data = data;
		temp->_next = NULL;
		if (this->_rear == NUll)
		{
			this->_rear = temp;
			this->_front = temp;
		}
		else
		{
			this->_rear->_next = temp;
			this->_rear = temp;
		}
	}
	catch (bad_alloc)
	{
		cerr << "error QueuePush" << '\n';
		return;
	}
}

MyQueue::QueuePop()
{
	assert(this->_front);
	QNode* t = this->_front->_next;
	delete this->_front;
	this->_front = t;
}


MyQueue::QueueFront()
{
	assert(this->_front);
	return this->_front->_data;
}

MyQueue::QueueBack()
{
	assert(this->_rear);
	return this->_rear->_data;
}

MyQueue::QueueSize()
{
	int ret = 0;
	QNode* cur = this->_front;
	while (cur)
	{
		ret++;
		cur = cur->_next;
	}
	return ret;
}


MyQueue::QueueEmpty()
{
	if (this->_front == NULL && this->_rear == NULL)
	{
		return 1;
	}
	return 0;
}

MyQueue::~MyQueue()
{
	QNode* cur = this->_front;
	while (cur)
	{
		QNode* t = cur->_next;
		delete this->_front;
		cur = t;
	}
}






